--[[
FilePath: \Lua\View\UICoupon\UICouponPop.lua
代金券支付弹框
--]]

require 'View/UIBase/UIBase'

UICouponPop = UIBase.New(PanelNames.UICouponPop)
local _M = UICouponPop

_M.hasInit = false
_M.func = nil
_M.payId = nil

function _M.Init(payId, func)
    _M.payId = payId;
    _M.func = func;
    UIBase.Init(_M)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("bgClose"), self.OnBack)
        self.name = self:GetChild("name");
        self.Desc = self:GetChild("Desc");

        self.btn_coupon1 = self:GetChild("btn_coupon1");  --代金券支付
        self.btn_coupon2 = self:GetChild("btn_coupon2");  --普通支付
        self.btn_coupon3 = self:GetChild("btn_coupon3");  --飞飞币支付

        self.btnQuite = self:GetChild("btnQuite");         --代金券快捷支付
        self.btnQuite1 = self:GetChild("btnQuite1");       --飞飞币快捷支付

        self.couponIcon = self:GetChild("getCouponIcon");
        self.couponText = self:GetChild("getCouponText");
        self.coupontips = self:GetChild("coupontips");
        self.moneyCtrl = self:GetController("IsMoney");

        self.couponIcon1 = self:GetChild("getCouponIcon1");
        self.couponText1 = self:GetChild("getCouponText1");
        self.coupontips1 = self:GetChild("coupontips1");
        self.petCtrl = self:GetController("IsPet");

        _M.hasInit = true

        self:addEvent()
    end

    self:initView()
end

function _M:addEvent()
    self.btn_coupon1.onClick:Add(self.onBtnCouponPay, self)
    self.btn_coupon2.onClick:Add(self.onBtnCommonPay,self)
    self.btn_coupon3.onClick:Add(self.onBtnPetPay,self)
    self.btnQuite.onClick:Add(self.onBtnQuite, self)
    self.btnQuite1.onClick:Add(self.onBtnQuite1, self)
end

function _M:removeEvent()
    self.btn_coupon1.onClick:Remove(self.onBtnCouponPay)
    self.btn_coupon2.onClick:Remove(self.onBtnCommonPay)
    self.btn_coupon3.onClick:Remove(self.onBtnPetPay)
    self.btnQuite.onClick:Remove(self.onBtnQuite)
    self.btnQuite1.onClick:Remove(self.onBtnQuite1)
end

function _M:onBtnCouponPay()
    if _M.func ~= nil then
        _M.func(1);
    end
    self:OnBack();
end

function _M:onBtnCommonPay()
    --logicMgr.CostManager.setPanelTipsState(true);
    if _M.func ~= nil then
        _M.func(2);
    end
    self:OnBack();
end

function _M:onBtnPetPay()
    if _M.func ~= nil then
        _M.func(3);
    end
    self:OnBack();
end

function _M:onBtnQuite()
    if self.btnQuite.selected then
        logicMgr.CostManager.setFastPayType(1);
    else
        logicMgr.CostManager.setFastPayType(0);
    end
    self.btnQuite1.selected = false;
    --logicMgr.CostManager.setPanelTipsState(not self.btnQuite.selected);
end

function _M:onBtnQuite1()
    if self.btnQuite1.selected then
        logicMgr.CostManager.setFastPayType(2);
    else
        logicMgr.CostManager.setFastPayType(0);
    end
    self.btnQuite.selected = false;
end

function _M:initView()
    if self.payId == nil then
        self:OnBack();
        return;
    end
    local pay_config = G.dataTable["t_pay"][tostring(self.payId)];
    if pay_config == nil then
        self:OnBack();
        return;
    end
    self.name.text = getLanguage("ActivityDesc1007")
    self.Desc.text = getLanguage("ActivityDesc1008")
    self.coupontips.text = getLanguage("ActivityDesc1009")
    self.coupontips1.text = getLanguage("ActivityDesc1009")
    self.btn_coupon1.text = getLanguage("ActivityDesc1010")
    self.btn_coupon2.text = getLanguage("ActivityDesc1011")
    self.btn_coupon3.text = getLanguage("ActivityDesc1013")

    self.btnQuite.text = getLanguage("ActivityDesc1012")
    self.btnQuite1.text = getLanguage("ActivityDesc1014")

    local price = tonumber(pay_config.f_Price);
    local cfg = G.dataTable["t_item"][tostring(200028)];
    if cfg ~= nil then
        self.couponIcon.url = EnumConst.AtlasPath.ItemAtlas .. cfg.f_Icon;
    end
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(200028))
    if tonumber(haveNum) < price then
        self.moneyCtrl.selectedIndex = 0;
        self.couponText.text = "[color=#FF2A4D]"..getNumString(haveNum).."[/color]/"..price;
    else
        self.moneyCtrl.selectedIndex = 1;
        self.couponText.text = getNumString(haveNum).."/"..price;
    end
    self.btnQuite.selected = false;

    --飞飞币
    local petPrice = tonumber(pay_config.f_PricePet);
    local petNum = logicMgr.ItemManager.getCurrencyCountByModelId(tonumber(35));
    cfg = G.dataTable["t_item"][tostring(35)];
    if cfg ~= nil then
        self.couponIcon1.url = EnumConst.AtlasPath.ItemAtlas .. cfg.f_Icon;
    end
    if tonumber(petNum) < petPrice then
        self.petCtrl.selectedIndex = 0;
        self.couponText1.text = "[color=#FF2A4D]"..getNumString(petNum).."[/color]/"..petPrice;
    else
        self.petCtrl.selectedIndex = 1;
        self.couponText1.text = getNumString(petNum).."/"..petPrice;
    end
    if tonumber(pay_config.f_Buy) == 0 then
        self.petCtrl.selectedIndex = 0;
    end
    self.btnQuite1.selected = false;
end

-- 显示
function _M:OnEnable()

end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    _M.hasInit = false
    _M.func = nil
    _M.payId = nil
end

return _M;